package src.view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.jgoodies.forms.layout.*;
import org.jdesktop.swingx.*;
/*
 * Created by JFormDesigner on Sat May 29 19:57:43 ART 2010
 */

import src.controller.ManageMailBoxesController;
import src.resource.PublicResource;




/**
 * @author Guillermo Exposito
 */
public class ManageMailBoxes extends JPanel {
	
	private final String PATH=PublicResource.getPath();
	private static final long serialVersionUID = 1L;
	private ManageMailBoxesController controller;
	private static ManageMailBoxes instance=null;
	
	public static ManageMailBoxes getInstance(ManageMailBoxesController controller){
		if(instance==null)
			instance=new ManageMailBoxes(controller);
		return instance;
	}
	
	private ManageMailBoxes(ManageMailBoxesController controller) {
		this.controller=controller;
		initComponents();
		this.btnSearch.addActionListener(new ActionSearchMailBox());
		this.btnDelete.addActionListener(new ActionDeleteMailBox());
		this.jListMailBoxes.addListSelectionListener(new ActionSelectItem());
		this.btnSave.addActionListener(new ActionUpdateMailBox());
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Guillermo Exposito
		panel2 = new JPanel();
		lblTitle = new JLabel();
		panel1 = new JPanel();
		jPanelSearchUser = new JPanel();
		lblSearchMailBox = new JLabel();
		label1 = new JLabel();
		txtSearch = new JTextField();
		btnSearch = new JButton();
		scrollPane1 = new JScrollPane();
		jListMailBoxes = new JList();
		btnDelete = new JButton();
		jPanelFields = new JPanel();
		lblName = new JLabel();
		txtName = new JTextField();
		label4 = new JLabel();
		lblPass = new JLabel();
		jPasswordFieldPass = new JPasswordField();
		label5 = new JLabel();
		lblConfirmPass = new JLabel();
		jPasswordFieldConfirmPass = new JPasswordField();
		label6 = new JLabel();
		label2 = new JLabel();
		chkActive = new JCheckBox();
		label3 = new JLabel();
		btnSave = new JButton();
		CellConstraints cc = new CellConstraints();

		//======== this ========

		setLayout(new FormLayout(
			"left:default:grow",
			"fill:default, fill:default:grow"));

		//======== panel2 ========
		{
			panel2.setLayout(new FormLayout(
				"left:440dlu",
				"10dlu, fill:default, 5dlu"));

			//---- lblTitle ----
			lblTitle.setText("Manage MailBoxes");
			lblTitle.setFont(new Font("Dialog", Font.PLAIN, 18));
			panel2.add(lblTitle, cc.xywh(1, 2, 1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));
		}
		add(panel2, cc.xy(1, 1));

		//======== panel1 ========
		{
			panel1.setLayout(new HorizontalLayout(1));

			//======== jPanelSearchUser ========
			{
				jPanelSearchUser.setLayout(new FormLayout(
					"20dlu, 22dlu, 3dlu, 100dlu, 3dlu, $lcgap, 62dlu, $lcgap, 5dlu",
					"2*(default, 10dlu), fill:default:grow, $lgap, 10dlu, $lgap, default, $lgap, 10dlu"));

				//---- lblSearchMailBox ----
				lblSearchMailBox.setText("Search MailBox");
				lblSearchMailBox.setFont(new Font("Dialog", Font.PLAIN, 14));
				jPanelSearchUser.add(lblSearchMailBox, cc.xywh(2, 1, 3, 1));

				//---- label1 ----
				label1.setText("Name");
				jPanelSearchUser.add(label1, cc.xywh(2, 3, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
				jPanelSearchUser.add(txtSearch, cc.xy(4, 3));

				//---- btnSearch ----
				btnSearch.setText("Search");
				jPanelSearchUser.add(btnSearch, cc.xywh(7, 3, 3, 1, CellConstraints.LEFT, CellConstraints.DEFAULT));

				//======== scrollPane1 ========
				{

					//---- jListMailBoxes ----
					jListMailBoxes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					scrollPane1.setViewportView(jListMailBoxes);
				}
				jPanelSearchUser.add(scrollPane1, cc.xywh(2, 5, 6, 1, CellConstraints.FILL, CellConstraints.FILL));

				//---- btnDelete ----
				btnDelete.setText("Delete");
				jPanelSearchUser.add(btnDelete, cc.xywh(7, 9, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
			}
			panel1.add(jPanelSearchUser);

			//======== jPanelFields ========
			{
				jPanelFields.setBorder(null);
				jPanelFields.setLayout(new FormLayout(
					"60dlu, $lcgap, 150dlu, 5dlu, 10dlu",
					"45dlu, 6*($lgap, default)"));

				//---- lblName ----
				lblName.setText("Name");
				lblName.setHorizontalAlignment(SwingConstants.RIGHT);
				jPanelFields.add(lblName, cc.xy(1, 3));
				jPanelFields.add(txtName, cc.xy(3, 3));

				//---- label4 ----
				label4.setText("*");
				label4.setForeground(Color.red);
				jPanelFields.add(label4, cc.xy(4, 3));

				//---- lblPass ----
				lblPass.setText("Pass");
				lblPass.setHorizontalAlignment(SwingConstants.LEFT);
				jPanelFields.add(lblPass, cc.xywh(1, 5, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
				jPanelFields.add(jPasswordFieldPass, cc.xy(3, 5));

				//---- label5 ----
				label5.setText("*");
				label5.setForeground(Color.red);
				jPanelFields.add(label5, cc.xy(4, 5));

				//---- lblConfirmPass ----
				lblConfirmPass.setText("Confirm pass");
				lblConfirmPass.setHorizontalAlignment(SwingConstants.CENTER);
				jPanelFields.add(lblConfirmPass, cc.xywh(1, 7, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
				jPanelFields.add(jPasswordFieldConfirmPass, cc.xy(3, 7));

				//---- label6 ----
				label6.setText("*");
				label6.setForeground(Color.red);
				jPanelFields.add(label6, cc.xy(4, 7));

				//---- label2 ----
				label2.setText("State");
				jPanelFields.add(label2, cc.xywh(1, 9, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));

				//---- chkActive ----
				chkActive.setText("active");
				jPanelFields.add(chkActive, cc.xy(3, 9));

				//---- label3 ----
				label3.setText("* Required fields");
				label3.setForeground(Color.red);
				jPanelFields.add(label3, cc.xy(3, 11));

				//---- btnSave ----
				btnSave.setText("Update name and pass");
				jPanelFields.add(btnSave, cc.xywh(3, 13, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
			}
			panel1.add(jPanelFields);
		}
		add(panel1, cc.xy(1, 2));
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
		lblTitle.setIcon(new ImageIcon(PATH+"mail-inbox.png"));
		btnSave.setIcon(new ImageIcon(PATH+"document-save.png"));
		btnSearch.setIcon(new ImageIcon(PATH+"system-search.png"));
		btnDelete.setIcon(new ImageIcon(PATH+"edit-delete.png"));
		this.listModelMailBoxes=new DefaultListModel();
		
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Guillermo Exposito
	private JPanel panel2;
	private JLabel lblTitle;
	private JPanel panel1;
	private JPanel jPanelSearchUser;
	private JLabel lblSearchMailBox;
	private JLabel label1;
	private JTextField txtSearch;
	private JButton btnSearch;
	private JScrollPane scrollPane1;
	private JList jListMailBoxes;
	private JButton btnDelete;
	private JPanel jPanelFields;
	private JLabel lblName;
	private JTextField txtName;
	private JLabel label4;
	private JLabel lblPass;
	private JPasswordField jPasswordFieldPass;
	private JLabel label5;
	private JLabel lblConfirmPass;
	private JPasswordField jPasswordFieldConfirmPass;
	private JLabel label6;
	private JLabel label2;
	private JCheckBox chkActive;
	private JLabel label3;
	private JButton btnSave;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
	private DefaultListModel listModelMailBoxes;

	public JPanel getjPanelSearchUser() {
		return jPanelSearchUser;
	}

	public void setjPanelSearchUser(JPanel jPanelSearchUser) {
		this.jPanelSearchUser = jPanelSearchUser;
	}

	public JTextField getTxtSearch() {
		return txtSearch;
	}

	public void setTxtSearch(JTextField txtSearch) {
		this.txtSearch = txtSearch;
	}

	public JButton getBtnSearch() {
		return btnSearch;
	}

	public void setBtnSearch(JButton btnSearch) {
		this.btnSearch = btnSearch;
	}

	public JList getjListMailBoxes() {
		return jListMailBoxes;
	}

	public void setjListMailBoxes(JList jListMailBoxes) {
		this.jListMailBoxes = jListMailBoxes;
	}

	public JButton getBtnDelete() {
		return btnDelete;
	}

	public void setBtnDelete(JButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	public JTextField getTxtName() {
		return txtName;
	}

	public void setTxtName(JTextField txtName) {
		this.txtName = txtName;
	}

	public JPasswordField getjPasswordFieldPass() {
		return jPasswordFieldPass;
	}

	public void setjPasswordFieldPass(JPasswordField jPasswordFieldPass) {
		this.jPasswordFieldPass = jPasswordFieldPass;
	}

	public JPasswordField getjPasswordFieldConfirmPass() {
		return jPasswordFieldConfirmPass;
	}

	public void setjPasswordFieldConfirmPass(JPasswordField jPasswordFieldConfirmPass) {
		this.jPasswordFieldConfirmPass = jPasswordFieldConfirmPass;
	}

	public JCheckBox getChkActive() {
		return chkActive;
	}

	public void setChkActive(JCheckBox chkActive) {
		this.chkActive = chkActive;
	}

	public JButton getBtnSave() {
		return btnSave;
	}

	public void setBtnSave(JButton btnSave) {
		this.btnSave = btnSave;
	}

	public DefaultListModel getListModelMailBoxes() {
		return listModelMailBoxes;
	}

	public void setListModelMailBoxes(DefaultListModel listModelMailBoxes) {
		this.listModelMailBoxes = listModelMailBoxes;
	}
	
	private class ActionSearchMailBox implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performSearchMailBox();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	private class ActionDeleteMailBox implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performDeleteMailBox();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	private class ActionUpdateMailBox implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performUpdateMailBox();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	private class ActionSelectItem implements ListSelectionListener{

		@Override
		public void valueChanged(ListSelectionEvent arg0) {
			try {
				controller.performSelectItem();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	
	
}